#ifndef _COMMON_H
#define _COMMON_H

#include <stdbool.h>
#include "syscore.h"
#include "apis.h"
#ifdef __cplusplus
extern "C"{
#endif
#define roundup8(x)  (((U32)(x)+7)&(~7))

typedef enum tagUNPACK_RESULT {
UNPACK_OK,
UNPACK_END,
UNPACK_ERROR
}UNPACK_RESULT;

extern U16 CM_paraPackU32(PPARAM pParam, U32 paraType, U32 paraValue);
extern U16 CM_paraPackError(PPARAM pParam, U32 errorCode);
extern U16 CM_paraPackException(PPARAM pParam, U32 exCode);

extern U32 CM_paraPackPara(PPARAM *ppParam, U32 paraType, U32 paraLength, U8** pParaValue);
extern UNPACK_RESULT CM_paraUnpackPara(PPARAM *ppParameters, U32 *length, U32* pParaType, U32* pParalength, U8** pParaValue );

extern RESULT CM_sendMsg(MSGHEADER * pMsgHeader,void * pMsgPayload);
extern RESULT CM_SendMsg_noPara(
    U16 dstID,
    U16 srcID,
    U16 msgType,
    U16 cmdType,
    U16 cmdCode,
    U16 cmdIndex);

extern RESULT CM_SendMsg_1Para(
    U16 dstID,
    U16 srcID,    
    U16 msgType,
    U16 cmdType,
    U16 cmdCode,
    U16 cmdIndex,
    U32 paraType,
    U32 paraLength,
    U8 *paraValue
    );

extern RESULT CM_SendMsg_2Para(
    U16 dstID,
    U16 srcID,
    U16 msgType,
    U16 cmdType,
    U16 cmdCode,
    U16 cmdIndex,
    U32 paraType1,
    U32 paraLength1,
    U8 *paraValue1,
    U32 paraType2,
    U32 paraLength2,
    U8 *paraValue2
    );

extern RESULT CM_SendMsg_3Para(
    U16 dstID,
    U16 srcID,
    U16 msgType,
    U16 cmdType,
    U16 cmdCode,
    U16 cmdIndex,
    U32 paraType1,
    U32 paraLength1,
    U8 *paraValue1,
    U32 paraType2,
    U32 paraLength2,
    U8 *paraValue2,
    U32 paraType3,
    U32 paraLength3,
    U8 *paraValue3
    );
extern RESULT CM_SendMsg_4Para(
    U16 dstID,
    U16 srcID,
    U16 msgType,
    U16 cmdType,
    U16 cmdCode,
    U16 cmdIndex,
    U32 paraType1,
    U32 paraLength1,
    U8 *paraValue1,
    U32 paraType2,
    U32 paraLength2,
    U8 *paraValue2,
    U32 paraType3,
    U32 paraLength3,
    U8 *paraValue3,
    U32 paraType4,
    U32 paraLength4,
    U8 *paraValue4
    );

extern RESULT CM_SendMsg_5Para(
    U16 dstID,
    U16 srcID,
    U16 msgType,
    U16 cmdType,
    U16 cmdCode,
    U16 cmdIndex,
    U32 paraType1,
    U32 paraLength1,
    U8 *paraValue1,
    U32 paraType2,
    U32 paraLength2,
    U8 *paraValue2,
    U32 paraType3,
    U32 paraLength3,
    U8 *paraValue3,
    U32 paraType4,
    U32 paraLength4,
    U8 *paraValue4,
    U32 paraType5,
    U32 paraLength5,
    U8 *paraValue5
    );

typedef struct tagCMMODINFO{
    U16 ModID;
    BOOL useTimer;
    void (*setProc)(PCMDHEADER pCmd, PCMDHEADER pAck);
    void (*getProc)(PCMDHEADER pCmd, PCMDHEADER pAck);
    void (*eventProc)(PCMDHEADER pCmd);
    void (*setAckProc)(PCMDHEADER pCmd);
    void (*getAckProc)(PCMDHEADER pCmd);
    void (*eventAckProc)(PCMDHEADER pCmd);
    void (*postProc)(void);
	
} CMMODINFO, *PCMMODINFO;

extern void CM_cmdDispatcher(CMMODINFO *pCmModInfo);

typedef ERROR_CODE (*PARADECODER)(void *h, U8* paraValue,U32 paraLength);

typedef struct tagPARADECODERMAP {
    U32 paraType;
    PARADECODER decoder;    
} PARADECODERMAP;
#if 0
typedef struct tagUEIDPARALIST {
    U64 uptime;
    int slotId;
    char *imsirecord;
    char *imeirecord;
}UEIDPARALIST;
#endif
#if 1
typedef struct tagUEIDPARALIST {
    U64 uptime;
    char *imsirecord;
    char *imeirecord;
    U8 slotId;
    U16 lac;
}UEIDPARALIST;
#endif
RESULT CM_paraListDecoder(PCMDHEADER pCmd,PCMDHEADER pAck, PARADECODERMAP *pDTable, U32 dTableSize,void* pValueStorage, BOOL bDiscardUnknownPara);

ERROR_CODE CM_decodeTimestamp(void *h, U8* paraValue, U32 paraLength );
ERROR_CODE CM_decodeIMSI(void *h, U8* paraValue, U32 paraLength );
ERROR_CODE CM_decodeIMEI(void *h, U8* paraValue, U32 paraLength );
ERROR_CODE CM_decodeStandard(void *h, U8* paraValue, U32 paraLength );
ERROR_CODE CM_decodeSlotId(void *h, U8* paraValue, U32 paraLength);
ERROR_CODE CM_decodeLac(void *h, U8* paraValue, U32 paraLength);
extern U16 gCmdIndex;

extern bool execShellCommand(const char* cmd, char* buf, int len);
pid_t getPidByName(char* task_name);
RESULT createRestartCauseFileByCode(U16 code);
void saveImportantLog(const char *log);
void saveGsmLog(const char *log);


#ifdef __cplusplus
}
#endif


#endif

